package com.g4m.baselib.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * Created on 2020-01-01
 * <p>Copyright 2008-2020 snsndk.com</p>
 *
 *
 */
@Setter
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SdkResponseListData<T> {
    /**
     * 当前页条数
     */
    private Integer count = 0;
    /**
     * 总条数
     */
    private Integer total = 0;
    /**
     * 当前页
     */
    private Integer page = 1;

    /**
     * 每页条数
     */
    private Integer limit = 20;

    /**
     * 总页数
     */
    private Integer totalPage = 0;

    /**
     * 是否还有更多数据
     */
    private Integer hasMore = 1;

    /**
     * 游标
     */
    private Long cursor;

    /**
     * 数据列表集合
     */
    private List<T> list;

    public SdkResponseListData() {

    }

    /**
     * 构造使用游标
     *
     * @param limit 条数
     * @param list  数据
     */
    public SdkResponseListData(Integer limit, List<T> list) {
        this.limit = limit;
        if (list == null || list.size() == 0) {
            this.list = list;
            this.cursor = 0L;
            this.hasMore = 0;
        } else {
            if (list.size() > limit) {
                this.hasMore = 1;
                list.remove(list.size() - 1);
                this.list = list;
            } else {
                this.hasMore = 0;
                this.list = list;
            }
        }
    }

    public SdkResponseListData(Integer total, Integer page, Integer limit, List<T> list) {
        this.total = total;
        this.page = page;
        this.limit = limit;
        this.list = list;
        this.totalPage = (int) Math.ceil((double) total / (double) limit);
        this.hasMore = totalPage > page ? 1 : 0;
    }

    public Integer getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public Integer getPage() {
        if (page < 1) {
            return 1;
        }
        return page;
    }

    public Integer getLimit() {
        if (limit < 1) {
            return 1;
        }
        return limit;
    }
}
